TK!2
=v
#1
:n
P
:v
130
:s
i
:c
periodic payment
#2
:n
V
:v
5000
:s
i
:c
loan principal
#3
:n
t
:v
4
:s
i
:c
term (in years)
#4
:n
N
:v
12
:s
i
:c
number of periods per year
#5
:n
x
:v
.1
:s
i
:c
independent variable (interest rate)
#6
:n
F
:v
-3.187082826263676
:s
o
:c
F(x)
#7
:n
y3
:v
240.1220342079568
:s
o
:c
F'(x)=dF/dx (numerical approximation)
#8
:n
u
:s
b
#9
:n
v
:s
b
#10
:n
du
:s
b
#11
:n
dv
:s
b
#12
:n
dF
:s
b
:c
value of symbolic F'(x)=dF/dx
#13
:n
g
:s
b
#14
:n
h
:s
b
#15
:n
a
:s
b
#16
:n
b
:s
b
#17
:n
c
:s
b
=u
=l
=f
#1
:n
diff13
:c
First derivative, 3-point formula
:t
Procedure
:i
fun,x,h
:o
z
#2
:n
fun
:t
Rule
:p
V,N,t,P
:a
x
:v
F
#3
:n
RATEL
:t
Procedure
:i
P,V,t,N
:o
x
#1
:n
diff13
:z
" Notation: fun  - name of the differentiated function
"           x    - value of the independent variable
"           h    - increment

z:= (apply(fun,x+h)-apply(fun,x-h))/(2*h)
/E
E
#2
:n
fun
:z
F = V/N*x/(1-(x/N+1)^-(N*t)) - P
/E
E
#3
:n
RATEL
:z
g:= -N*t                                  " (10)
h:= V/N                                   " (11)
x:= .1
L: x0:= x
   a:= x/N + 1                            " (12)
   b:= a^g                                " (13)
   c:= 1 - b                              " (14)
   x:= x - (x-P*c/h)/(1-x*t*b/(a*c))      " (2,a)
   if abs(x-x0)>1e-6 goto L
/E
E
=r
#1
:r
" ***  Symbolic  differentiation  exercise  ***
:s
C
#3
:r
" The current TK Solver Plus does not perform symbolic differentiation or,
:s
C
#4
:r
" generally, symbolic manipulation.  The concept of inversion or inverse
:s
C
#5
:r
" operations is nearly all TK knows of mathematics; it uses it cleverly in
:s
C
#6
:r
" the equation solving process, and also during this exercise.
:s
C
#8
:r
" Sometimes, however, one needs to perform symbolic manipulation, and, in
:s
C
#9
:r
" particular, symbolic differentiation.  This exercise shows how TK can
:s
C
#10
:r
" be used as a scratch pad and a sort of a theorem prover for that purpose.
:s
C
#12
:r
" Here is an example: We wanted to develop a function which would evaluate
:s
C
#13
:r
" an unknown interest rate  x  from the loan formula
:s
C
#14
:r
"
:s
C
#15
:r
"                 V               x
:s
C
#16
:r
"         P  =  ÄÄÄÄÄ  ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ                        (1)
:s
C
#17
:r
"                 N                       - N t
:s
C
#18
:r
"                         1  -  (x/N + 1)
:s
C
#19
:r
"
:s
C
#20
:r
" without the use of the Iterative Solver.  We decided to build a procedure
:s
C
#21
:r
" function based on iterative improvement of the initial guess using the
:s
C
#22
:r
" Newton's formula:
:s
C
#23
:r
"                                         F ( x  )
:s
C
#24
:r
"                                              i
:s
C
#25
:r
"                      x      =   x  -  ÄÄÄÄÄÄÄÄÄÄÄÄÄÄ                   (2)
:s
C
#26
:r
"                        i+1       i      F ' ( x  )
:s
C
#27
:r
"                                                i
:s
C
#28
:r
" In our case
:s
C
#29
:r
"                            V               x
:s
C
#30
:r
"              F ( x )  =  ÄÄÄÄÄ  ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ  -  P       (3)
:s
C
#31
:r
"                            N                       - N t
:s
C
#32
:r
"                                    1  -  (x/N + 1)
:s
C
#33
:r
" or, written as TK rule,
:s
C
#35
:r
F = V/N * x/(1-(x/N+1)^-(N*t)) - P    " (3,a)
#37
:r
" where  V,P,t,N  are constants standing for the loan principal, periodic
:s
C
#38
:r
"                 payment, term and number of periods per year.
:s
C
#40
:r
" We could use a numeric differentiation technique, as does TK's Iterative
:s
C
#41
:r
" Solver, for evaluating the derivative F'(xi) at each iteration step.
:s
C
#42
:r
" However, it is more efficient computationally to use a symbolic derivative.
:s
C
#43
:r
" We copy the three-point numeric differentiation procedure 'diff13' from the
:s
C
#44
:r
" TK library model NUMDIF in the directory DIFINT anyway, to check a
:s
C
#45
:r
" symbolic derivative which we are going to develop.  Here is the rule using 
:s
C
#46
:r
" the function 'diff13' to approximate the symbolic derivative:
:s
C
#48
:r
y3 = diff13('fun,x,.00001)            " (4)
#50
:r
" We have to define the function 'fun'.  A rule function interpretation of
:s
C
#51
:r
" the formula (2) with  x  as an argument variable and  V,P,t,N  as para- 
:s
C
#52
:r
" meter variables will do (see the Function Subsheet).
:s
C
#54
:r
" We can now assign values of  V,P,t,N,x  and solve for  F(x)  and  F'(x)÷y3.
:s
C
#55
:r
" (See the Variable Sheet; there are some other variables too.  They are used
:s
C
#56
:r
"  in the following rules.  The rules below are cancelled at this point and
:s
C
#57
:r
"  thus the additional variables are not evaluated.  You can uncancel them
:s
C
#58
:r
"  as you follow this story.)
:s
C
#60
:r
" Now we are coming to symbolic differentiation.  We introduce two new
:s
C
#61
:r
" functions of  x ,  u(x)  and  v(x) :
:s
C
#63
:r
u = V/N * x                            " (5)
:s
C
#64
:r
v = 1 - (x/N+1)^-(N*t)                 " (6)
:s
C
#66
:r
" and rewrite rule (3):
:s
C
#68
:r
F = u/v - P                            " (3,b)
:s
C
#70
:r
" We solve again; TK doesn't detect any inconsistency and that means that
:s
C
#71
:r
" the substitutions were correct so far.
:s
C
#73
:r
" The derivative of a quotient is given by:
:s
C
#74
:r
"                                               u' v  -  u v'
:s
C
#75
:r
"                                  (u/v)'  =  ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ          
:s
C
#76
:r
"                                                    v ý
:s
C
#78
:r
" The apostrophe  '  cannot be used in variable names.  So, we will denote
:s
C
#79
:r
" the derivatives with respect to  x  by a prefix  d :
:s
C
#81
:r
du = V/N                               " (7)
:s
C
#82
:r
dv = t * (x/N+1)^-(N*t+1)              " (8)
:s
C
#83
:r
dF = (du*v - u*dv) / v^2               " (9)
:s
C
#85
:r
" We press F9 again and find from the Variable Sheet that the values of  y3 
:s
C
#86
:r
" and  dF  are identical up to the 10th digit; we take it as a proof of the
:s
C
#87
:r
" correctness of the derivative (9).
:s
C
#89
:r
" In the Newton's iteration we actually need the most efficient way to
:s
C
#90
:r
" evaluate  F/dF  repeatedly for a succession of values of  x .  We bring
:s
C
#91
:r
" relevant equations together (by moving or copying them) to see what can
:s
C
#92
:r
" be done:
:s
C
#94
:r
F = u/v - P                            " (3,b)
:s
C
#95
:r
u = V/N * x                            " (5)
:s
C
#96
:r
v = 1 - (x/N+1)^-(N*t)                 " (6)
:s
C
#97
:r
du = V/N                               " (7)
:s
C
#98
:r
dv = t * (x/N+1)^-(N*t+1)              " (8)
:s
C
#99
:r
dF = (du*v - u*dv) / v^2               " (9)
:s
C
#101
:r
" First, we introduce two intermediate variables that can be defined outside
:s
C
#102
:r
" the iteration loop, copy the equations (5-8), and make the substitutions:
:s
C
#104
:r
g = -N*t                               " (10)
:s
C
#105
:r
h = V/N                                " (11)
:s
C
#106
:r
u = h * x                              " (5,a)
:s
C
#107
:r
v = 1 - (x/N+1)^g                      " (6,a)
:s
C
#108
:r
du = h                                 " (7,a)
:s
C
#109
:r
dv = t * (x/N+1)^(g+1)                 " (8,a)
:s
C
#111
:r
" We press F9 to solve and check the correctness of the substitutions.
:s
C
#112
:r
" Ooops ... the last equation is marked inconsistent.  The correct exponent
:s
C
#113
:r
" is  g-1 .  We edit the equation and solve again. Everything is OK, and we
:s
C
#114
:r
" can move on.
:s
C
#116
:r
" We introduce three more new variables to represent the repetitive (i.e.
:s
C
#117
:r
" x-dependent) terms, and we make appropriate substitutions:
:s
C
#119
:r
a = x/N + 1                            " (12)
:s
C
#120
:r
b = a^g                                " (13)
:s
C
#121
:r
c = 1 - b                              " (14)
:s
C
#122
:r
u = h * x                              " (5,b)
:s
C
#123
:r
v = c                                  " (6,b)
:s
C
#124
:r
du = h                                 " (7,b)
:s
C
#125
:r
dv = t * b/a                           " (8,b)
:s
C
#127
:r
" Now it is time to eliminate  u,v,du,dv  and introduce  F/dF :
:s
C
#129
:r
F = h*x/c - P                          " (3,c)
:s
C
#130
:r
dF = (h*c - h*x*t*b/a) / c^2           " (9,a)
:s
C
#131
:r
F/dF = (h*x/c-P)/((h*c-h*x*t*b/a)/c^2) " (15)
:s
C
#133
:r
" Needless to say, we press F9 after entering every equation to check on
:s
C
#134
:r
" the correctness of the substitutions and manipulations.
:s
C
#135
:r
" There are two more copy-edit-check cycles to perform on the equation (15):
:s
C
#136
:r
" multiplying both the denominator and the numerator on the right hand side
:s
C
#137
:r
" by  c^2 , and dividing them by  h*c :
:s
C
#139
:r
F/dF = (h*c*x-P*c^2)/(h*c-h*x*t*b/a)   " (15,a)
:s
C
#140
:r
F/dF = (x-P*c/h)/(1-x*t*b/(a*c))       " (15,b)
:s
C
#142
:r
" We can collect here (by copying) all the equations needed for the Newton's
:s
C
#143
:r
" iteration, and precede them by  "  so that they can be copied at once to
:s
C
#144
:r
" the procedure function 'RATEL'.  The equation (15,b) is edited into the
:s
C
#145
:r
" form of the Newton's formula (2):
:s
C
#147
:r
"g = -N*t                               " (10)
:s
C
#148
:r
"h = V/N                                " (11)
:s
C
#149
:r
"a = x/N + 1                            " (12)
:s
C
#150
:r
"b = a^g                                " (13)
:s
C
#151
:r
"c = 1 - b                              " (14)
:s
C
#152
:r
"x = x - (x-P*c/h)/(1-x*t*b/(a*c))      " (2,a)
:s
C
#154
:r
" See the Procedure Function Subsheet for 'RATEL'.  The equal signs in the
:s
C
#155
:r
" above lines were replaced by the assignment operators := , and the function
:s
C
#156
:r
" was completed with the standard guess value for  x  and the iteration loop.
:s
C
#158
:r
" You can try the function from this or any other sheet or subsheet.  Type
:s
C
#160
:r
"                 / E E RATEL(130,5000,4,12) [Enter]
:s
C
#162
:r
" TK promptly responds with the resulting rate .113175... on the Prompt/Error
:s
C
#163
:r
" line.
:s
C
#165
:r
" Notes: 1. The RATEL function developed here is a part of the TK Library
:s
C
#166
:r
"           model FILCAL2 in the FINANCE subdirectory.
:s
C
#167
:r
"        2. The TK way of proving the correctness of symbolic manipulations
:s
C
#168
:r
"           by solving a model with synonymic equations is very useful, but
:s
C
#169
:r
"           not quite rigorous from the mathematical viewpoint.  One has to
:s
C
#170
:r
"           be careful in selecting the testing values, avoid special values,
:s
C
#171
:r
"           such as 0 or 1, and, if in doubt, run the test through several
:s
C
#172
:r
"           sets of values.
:s
C
=p
=t
=n
=g
#1
:c
.000001
:i
10
:v
Yes
:l
60
:f
66
:m
0
:w
80
:b
Yes
:a
Yes
:r
Yes
:h
No
:t
1
:n
Yes
:d
LPT1
%Tr,f,0,1,0,1,0
